package com.neusoft.chess;

import javax.swing.*;
import java.awt.*;
import java.io.File;

public abstract class Chess {
    private  String name;//名称
    private  int x;//x的坐标，指定图片绘制位置
    private int y;//y的坐标
    private Point p;//计算坐标值
    private static  final int size = 30;//大小
    private static final int space = 40;//间隔
    private static final  int margin=20;//边距
    private static final int player_red=0;//红方
    private static final int player_black=1;//黑方
    private int player;//阵营
    private String folder = "pic"+ File.separator;
    private String suffix=".png";

    public void draw(Graphics g, JPanel panel){
        Image img =Toolkit.getDefaultToolkit().getImage(folder+name +player+suffix);
        g.drawImage(img,x,y,size,size,panel);
    }
    public Chess(String name,Point p, int player){
        this.name=name;
        setP(p);
        this.player=player;
    }

    /**
     * 计算坐标的公式
     */
   public void calXY(){
        x=margin-size/2+space*(p.x-1);
        y=margin-size/2+space*(p.y-1);

   }

    /**
     * 判断棋子能否从原坐标p移动目标点t
     * @param t
     * @return
     */
   public abstract  boolean isAbleMove(Point t);
    /**
     * 	根据点击的坐标xy生成Point坐标
     * @param x
     * @param y
     * @return
     */
    public static Point getPointFromXY(int x, int y) {
        Point p = new Point();
        p.x = (x - margin + size / 2) / space + 1;
        p.y = (y - margin + size / 2) / space + 1;

        return p;
    }

    /**
     * 反转坐标
     */
    public void reserve() {
        p.x = 10 - p.x;
        p.y = 11 - p.y;
        setP(p);
    }
    //在类中属性一般是不允许直接在其它类中直接使用的，建议使用get和set方法来设置获取属性值
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setP(Point p) {
        this.p = p;
        calXY();
    }

    public Point getP() {
        return this.p;
    }

    public int getPlayer() {
        return player;
    }

    public void setPlayer(int player) {
        this.player = player;
    }

    /*
     * toString()方法是属于Object类的
     * 	由子类重写
     */
    @Override
    public String toString() {
        //super.toString()：调用父类中toString()方法
//		return super.toString();
        return "Chess [name=" + name + ", x=" + x + ", y=" + y + ", p=" + p + ", size=" + size + ", space=" + space
                + ", margin=" + margin + ", player=" + player + ", folder=" + folder + ", suffix=" + suffix + "]";
    }

}
